<?php

namespace adminmatrix\plugin\command\migrate;
use Phinx\Migration\MigrationInterface;
use think\console\Input;
use think\console\input\Option as InputOption;
use think\console\Output;
use adminmatrix\plugin\command\Migrate;
class Run extends Migrate
{
    protected function configure()
    {
        $this->setName("plugin:migrate_run")
            ->addArgument("plugin", 3 ,   "导入的插件名称")
            ->setDescription('迁移数据库')
            ->addOption("--target", '-t', \think\console\input\Option::VALUE_REQUIRED, "执行迁移的版本")
            ->addOption('--date', '-d', InputOption::VALUE_REQUIRED, '执行迁移的时间')
            ->setHelp(<<<EOT
<info>php think plugin:migrate_run --plugin=ceshi </info>
<info>php think plugin:migrate_run --p ceshi </info>

<info>php think plugin:migrate_run --plugin=ceshi </info>
<info>php think migrate:run --plugin=ceshi -t 20110103081132</info>
<info>php think migrate:run --plugin=ceshi -d 20110103</info>
<info>php think migrate:run --plugin=ceshi -v</info>
EOT
            );
        $this->addOption('connection', 'c', InputOption::VALUE_REQUIRED, 'The database connection to use.');

    }

    protected function execute(Input $input, Output $output)
    {
        $pluginName = $this->pluginName = $input->getArgument("plugin");
        $version = $input->getOption('target');
        $date    = $input->getOption('date');
        $start = microtime(true);
        if (null !== $date) {
            $this->migrateToDateTime(new \DateTime($date));
        } else {
            $this->migrate($version);
        }
    }

    protected function migrate($version = null)
    {
        $migrations = $this->getMigrations();
        $versions   = $this->getVersions();
        $current    = $this->getCurrentVersion();

        if (empty($versions) && empty($migrations)) {
            return;
        }

        if (null === $version) {
            $version = max(array_merge($versions, array_keys($migrations)));
        } else {
            if (0 != $version && !isset($migrations[$version])) {
                $this->output->writeln(sprintf('<comment>warning</comment> %s is not a valid version', $version));
                return;
            }
        }

        // are we migrating up or down?
        $direction = $version > $current ? MigrationInterface::UP : MigrationInterface::DOWN;

        if ($direction === MigrationInterface::DOWN) {
            // run downs first
            krsort($migrations);
            foreach ($migrations as $migration) {
                if ($migration->getVersion() <= $version) {
                    break;
                }

                if (in_array($migration->getVersion(), $versions)) {
                    $this->executeMigration($migration, MigrationInterface::DOWN);
                }
            }
        }

        ksort($migrations);
        foreach ($migrations as $migration) {
            if ($migration->getVersion() > $version) {
                break;
            }

            if (!in_array($migration->getVersion(), $versions)) {
                $this->executeMigration($migration, MigrationInterface::UP);
            }
        }
    }

    protected function getCurrentVersion()
    {
        $versions = $this->getVersions();
        $version  = 0;

        if (!empty($versions)) {
            $version = end($versions);
        }

        return $version;
    }

}